﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Eqwww.Domain.Models.SystemManage;
using Eqwww.Code;

namespace Eqwww.TagHelpers
{
    public class ModuleButtonsTagHelper: Microsoft.AspNetCore.Razor.TagHelpers.TagHelper
    {
        public List<ModuleButtonModel> Buttons { get; set; }

        public bool? IsFloat { get; set; }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {

            output.TagName = "div";

            if (IsFloat == true)
            {
                var sb = new StringBuilder();
                sb.Append("<a href='javascript:;' class='mdui-btn mdui-ripple' mdui-menu='{target: \"#eq-operate-action-menu\"}'>");
                sb.Append("<i class=\"mdui-icon material-icons\">more_vert</i>操作</a>");
                sb.Append("<ul class=\"mdui-menu\" id=\"eq-operate-action-menu\">");
                if (Buttons != null)
                {
                    foreach (var item in Buttons)
                    {
                        if (item.ModuleButtonShowType != ModuleButtonShowType.Hide)
                        {
                            sb.Append("<li class=\"mdui-menu-item\">");
                            sb.Append("<a href = \"javascript:;\" onclick=\"" + item.JsAction + "\" class=\"eq-operate-btn " + GetModuleButtonColor(item.ModuleButtonColorType) + "\"><i class='mdui-icon material-icons'>" + item.Icon + "</i> " + item.Name.Trim() + "</a>");
                            sb.Append("</li>");
                        }
                        
                    }
                }
                sb.Append("</ul>");
                output.Content.SetHtmlContent(sb.ToString());
            }
            else
            {
                var sb = new StringBuilder();
                if (Buttons != null)
                {
                    foreach (var item in Buttons)
                    {
                        if (item.ModuleButtonShowType != ModuleButtonShowType.Hide)
                            sb.Append("<a href=\"javascript:; \" onclick=\"" + item.JsAction + "\" class=\"eq-operate-btn mdui-btn mdui-ripple " + GetModuleButtonColor(item.ModuleButtonColorType) + " \"><i class='mdui-icon material-icons'>" + item.Icon + "</i> " + item.Name.Trim() + "</a>");
                    }
                }
                output.Content.SetHtmlContent(sb.ToString());
            }


        }
        private string GetModuleButtonColor(ModuleButtonColorType? moduleButtonColorType)
        {
            if (!moduleButtonColorType.HasValue)
            {
                return "mdui-text-color-theme";
            }

            if (moduleButtonColorType == ModuleButtonColorType.ColorTheme)
            {
                return "mdui-text-color-theme";
            }
            else if (moduleButtonColorType == ModuleButtonColorType.ColorAccent)
            {
                return "mdui-text-color-theme-accent";
            }
            else if (moduleButtonColorType == ModuleButtonColorType.ColorDanger)
            {
                return "mdui-text-color-pink";
            }
            else if (moduleButtonColorType == ModuleButtonColorType.ColorWarning)
            {
                return "mdui-text-color-orange";
            }
            return "mdui-text-color-theme";
        }
    }
}
